import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';

import 'log.dart';

class Config {
  Config({
    required this.id,
    this.uin = '',
    this.delay = 1000,
    this.repeat = 5,
    this.hostUrl = _defaultHostUrl,
    this.ext1,
    this.ext2,
    this.ext3,
    this.aid = '',
    int? networkType,
    this.enablePageLoadTime = false,
    String? version, // 默认传入aegis sdk版本
  }) {
    url = '$hostUrl/collect';
    whiteListUrl = '$hostUrl/collect/whitelist';
    pvUrl = '$hostUrl/collect/pv';
    eventUrl = '$hostUrl/collect/events';
    customTimeUrl = '$hostUrl/speed/custom';
    performanceUrl = '$hostUrl/speed/performance';
    webvitalsUrl = '$hostUrl/speed/webvitals';
    this.networkType = networkType ?? NetworkType.unknown;

    /// 若未设定[version]则从pubspec.yaml文件读取aegis version
    if (version == null || version == '') {
      _setAegisVersion();
    } else {
      this.version = version;
    }
  }

  /// id或者密钥标识符，用来分辨上报项目
  String id;

  /// 用户标示符号
  String? uin;

  /// 本次上报的版本号码
  String version = '';

  String aid = '';

  /// 网络类型
  late int networkType;

  /// 上报延迟时间(ms)，改时间内的所有上报将会合并上报
  int delay;

  /// 重复上报次数(对于同一个错误超过多少次不上报)
  int repeat;

  /// 允许用户配置 hostUrl 默认值，如果用户又配置了 url 等信息，将会以 url 更细粒度为准
  String hostUrl = _defaultHostUrl;

  /// 是否启用页面渲染加载耗时计算上报，默认 false
  bool enablePageLoadTime;

  /// 上报url
  late String url;

  /// 请求白名单的url
  late String whiteListUrl;

  /// 上报pv的url
  late String pvUrl;

  /// 上报eventUrl的url
  late String eventUrl;

  /// 自定义测速上报
  late String customTimeUrl;

  /// 页面性能相关数据
  late String performanceUrl;

  /// webvitals 页面加载耗时上报的url
  late String webvitalsUrl;

  String? ext1;
  String? ext2;
  String? ext3;

  /// id或者密钥标识符，用来分辨上报项目
  static const String _defaultHostUrl = 'https://aegis.qq.com';

  Future<void> _setAegisVersion() async {
    WidgetsFlutterBinding.ensureInitialized();
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    version = packageInfo.version;
  }

  void setAid(String aid) {
    this.aid = aid;
  }
}
